using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentRegisterationTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentRegisterationTable objects.
    /// </summary>
    public class StudentRegisterationTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentRegisterationTable from the underlying datasource.
        /// </summary>
        /// <param name="sRID">The unique SRID of the StudentRegisterationTable in the database.</param>
        /// <returns>An StudentRegisterationTable when the SRID was found in the database, or null otherwise.</returns>
        public static StudentRegisterationTable GetItem(int sRID)
        {
            StudentRegisterationTable myStudentRegisterationTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentRegisterationTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@sRID", sRID);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentRegisterationTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentRegisterationTable;
            }
        }

        /// <summary>
        /// Gets an instance of StudentRegisterationTable from the underlying datasource.
        /// </summary>
        /// <param name="sRID">The unique SRID of the StudentRegisterationTable in the database.</param>
        /// <returns>An StudentRegisterationTable when the SRID was found in the database, or null otherwise.</returns>
        public static StudentRegisterationTable GetItem(int SchoolKey, int UserAccountKey, int StudentProfileKey, int EducationPeriodKey, int StandardLevelKey, int ClassRoomKey)
        {
            StudentRegisterationTable myStudentRegisterationTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentRegisterationTableCheckSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
                    myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
                    myCommand.Parameters.AddWithValue("@studentProfileKey", StudentProfileKey);
                    myCommand.Parameters.AddWithValue("@educationPeriodKey", EducationPeriodKey);
                    myCommand.Parameters.AddWithValue("@standardLevelKey", StandardLevelKey);
                    myCommand.Parameters.AddWithValue("@classRoomKey", ClassRoomKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentRegisterationTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentRegisterationTable;
            }
        }

        /// <summary>
        /// Returns a list with StudentRegisterationTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentRegisterationTable objects.</returns>
        public static StudentRegisterationTableList GetList()
        {
            StudentRegisterationTableList tempList = new StudentRegisterationTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentRegisterationTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        public static DataTable GetListByTimeTableKey(int SchoolKey, int LocationKey, int ClassRoomKey, int StandardLevelKey, int EducationPeriodKey, int TimeTableKey)
        {
            //[sprocStudentRegisterationTableSelectListByTimeTableKey]
            DataTable tblStudenRegisterList = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentRegisterationTableSelectListByTimeTableKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
                    myCommand.Parameters.AddWithValue("@locationKey", LocationKey);
                    myCommand.Parameters.AddWithValue("@classRoomKey", ClassRoomKey);
                    myCommand.Parameters.AddWithValue("@standardLevelKey", StandardLevelKey);
                    myCommand.Parameters.AddWithValue("@educationPeriodKey", EducationPeriodKey);
                    myCommand.Parameters.AddWithValue("@timeTableKey", TimeTableKey);

                    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
                    myConnection.Open();

                    tblStudenRegisterList = new DataTable("tblStudenRegisterList");
                    myAdapter.Fill(tblStudenRegisterList);

                    myConnection.Close();
                }
                return tblStudenRegisterList;
            }
        }

        /// <summary>
        /// Saves a StudentRegisterationTable in the database.
        /// </summary>
        /// <param name="myStudentRegisterationTable">The StudentRegisterationTable instance to save.</param>
        /// <returns>The new SRID if the StudentRegisterationTable is new in the database or the existing SRID when an item was updated.</returns>
        public static int Save(StudentRegisterationTable myStudentRegisterationTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentRegisterationTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentRegisterationTable.SRID == -1)
                    {
                        myCommand.Parameters.AddWithValue("@sRID", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@sRID", myStudentRegisterationTable.SRID);
                    }
                    if (myStudentRegisterationTable.UserAccountKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", myStudentRegisterationTable.UserAccountKey);
                    }
                    if (myStudentRegisterationTable.SchoolKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", myStudentRegisterationTable.SchoolKey);
                    }
                    if (myStudentRegisterationTable.SchoolMajorKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorKey", myStudentRegisterationTable.SchoolMajorKey);
                    }
                    if (myStudentRegisterationTable.StudentProfileKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@studentProfileKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@studentProfileKey", myStudentRegisterationTable.StudentProfileKey);
                    }
                    if (myStudentRegisterationTable.StandardLevelKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", myStudentRegisterationTable.StandardLevelKey);
                    }
                    if (myStudentRegisterationTable.LocationKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@locationKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@locationKey", myStudentRegisterationTable.LocationKey);
                    }

                    if (myStudentRegisterationTable.ClassRoomKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@classRoomKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@classRoomKey", myStudentRegisterationTable.ClassRoomKey);
                    }
                    if (myStudentRegisterationTable.EducationPeriodKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodKey", myStudentRegisterationTable.EducationPeriodKey);
                    }
                    if (myStudentRegisterationTable.TimeTableKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@timeTableKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@timeTableKey", myStudentRegisterationTable.TimeTableKey);
                    }
                    if (string.IsNullOrEmpty(myStudentRegisterationTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myStudentRegisterationTable.Remark);
                    }

                    myCommand.Parameters.AddWithValue("@status", myStudentRegisterationTable.Status);

                    //if (myStudentRegisterationTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myStudentRegisterationTable.CreatedDateTime);
                    //}
                    //if (myStudentRegisterationTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentRegisterationTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentRegisterationTable from the database.
        /// </summary>
        /// <param name="sRID">The SRID of the StudentRegisterationTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int sRID)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentRegisterationTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@sRID", sRID);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentRegisterationTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentRegisterationTable FillDataRecord(IDataRecord myDataRecord)
        {
            StudentRegisterationTable myStudentRegisterationTable = new StudentRegisterationTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SRID")))
            {
                myStudentRegisterationTable.SRID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SRID"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
            {
                myStudentRegisterationTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                myStudentRegisterationTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolMajorKey")))
            {
                myStudentRegisterationTable.SchoolMajorKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolMajorKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StudentProfileKey")))
            {
                myStudentRegisterationTable.StudentProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StudentProfileKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandardLevelKey")))
            {
                myStudentRegisterationTable.StandardLevelKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StandardLevelKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationKey")))
            {
                myStudentRegisterationTable.LocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("LocationKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomKey")))
            {
                myStudentRegisterationTable.ClassRoomKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("ClassRoomKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EducationPeriodKey")))
            {
                myStudentRegisterationTable.EducationPeriodKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EducationPeriodKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TimeTableKey")))
            {
                myStudentRegisterationTable.TimeTableKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TimeTableKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myStudentRegisterationTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStudentRegisterationTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentRegisterationTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentRegisterationTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentRegisterationTable;
        }
    }

}
